; file: 

; asm_io.inc was created by the textbook's authors.  It includes i/o functions
%include "asm_io.inc"

; Initialized output data
segment .data

prompt1 db "Give 1st number: ", 0
prompt2 db "Give 2nd number: ", 0
prompt3 db "Give 3rd number: ", 0
prompt4 db "Give 4th number: ", 0
prompt5 db "Give 5th number: ", 0
outmsg1 db ",", 0
outmsg2 db "Signed Bubble Sort: ", 0
outmsg3 db "Unsigned Bubble Sort: ", 0

; Uninitialized data for storing inputs
segment .bss

; Signed variables to work with
input1s resd 1
input2s resd 1
input3s resd 1
input4s resd 1
input5s resd 1

; Unsigned variables to work with
input1u resd 1
input2u resd 1
input3u resd 1
input4u resd 1
input5u resd 1

; Code for program to run
segment .text
	global asm_main
	
asm_main:
	enter 0,0
	pusha

; Prompt for, read and store inputs

	mov eax, prompt1 	; load prompt
	call print_string

	call read_int		; read and store int 1
	mov [input1s], eax	
	mov [input1u], eax

	mov eax, prompt2 	; load prompt
	call print_string

	call read_int		; read and store int 2
	mov [input2s], eax	
	mov [input2u], eax

	mov eax, prompt3 	; load prompt
	call print_string

	call read_int		; read and store int 3
	mov [input3s], eax
	mov [input3u], eax	

	mov eax, prompt4 	; load prompt
	call print_string

	call read_int		; read and store int 4
	mov [input4s], eax	
	mov [input4u], eax

	mov eax, prompt5 	; load prompt
	call print_string

	call read_int		; read and store int 5
	mov [input5s], eax	
	mov [input5u], eax


; SIGNED Bubble Sort
	
	mov eax, outmsg2
	call print_string
	call print_nl
			
	while_loops:
		mov edx, 0		; swapped := false
	
		mov ebx, [input1s]
		cmp ebx, [input2s]		; if A[1] > A[2]
		jle after_if1s
		mov ebx, [input1s]			; swap ( A[1], A[2] )
		mov ecx, [input2s]
		mov [input1s], ecx
		mov [input2s], ebx
		mov edx, 1
		
		after_if1s:
			mov ebx, [input2s]
			cmp ebx, [input3s]		; if A[2] > A[3]
			jle after_if2s
			mov ebx, [input2s]			; swap ( A[2], A[3] )
			mov ecx, [input3s]
			mov [input2s], ecx
			mov [input3s], ebx  
			mov edx, 1
		
		after_if2s:
			mov ebx, [input3s]
			cmp ebx, [input4s]		; if A[3] > A[4]
			jle after_if3s
			mov ebx, [input3s]			; swap ( A[3], A[4] )
			mov ecx, [input4s]
			mov [input3s], ecx
			mov [input4s], ebx 
			mov edx, 1 
		
		after_if3s:
			mov ebx, [input4s]
			cmp ebx, [input5s]		; if A[4] > A[5]
			jle after_if4s
			mov ebx, [input4s]			; swap ( A[4], A[5] )
			mov ecx, [input5s]
			mov [input4s], ecx
			mov [input5s], ebx
			mov edx, 1  
		
		after_if4s:			; Output results
			
			mov eax, [input1s]
			call print_int
			mov eax, outmsg1
			call print_string
			
			mov eax, [input2s]
			call print_int
			mov eax, outmsg1
			call print_string
			
			mov eax, [input3s]
			call print_int
			mov eax, outmsg1
			call print_string
			
			mov eax, [input4s]
			call print_int
			mov eax, outmsg1
			call print_string
			
			mov eax, [input5s]
			call print_int
			mov eax, outmsg1
			call print_string

			call print_nl
			
			cmp edx, 1		; Repeat if anything was previously swapped
			je while_loops
			
; UNSIGNED Bubble Sort
	mov eax, outmsg3
	call print_string
	call print_nl
	
	while_loopu:
		mov edx, 0		; swapped := false
	
		mov ebx, [input1u]
		cmp ebx, [input2u]		; if A[1] > A[2]
		jbe after_if1u
		mov ebx, [input1u]			; swap ( A[1], A[2] )
		mov ecx, [input2u]
		mov [input1u], ecx
		mov [input2u], ebx
		mov edx, 1
		
		after_if1u:
			mov ebx, [input2u]
			cmp ebx, [input3u]		; if A[2] > A[3]
			jbe after_if2u
			mov ebx, [input2u]			; swap ( A[2], A[3] )
			mov ecx, [input3u]
			mov [input2u], ecx
			mov [input3u], ebx  
			mov edx, 1
		
		after_if2u:
			mov ebx, [input3u]
			cmp ebx, [input4u]		; if A[3] > A[4]
			jbe after_if3u
			mov ebx, [input3u]			; swap ( A[3], A[4] )
			mov ecx, [input4u]
			mov [input3u], ecx
			mov [input4u], ebx 
			mov edx, 1 
		
		after_if3u:
			mov ebx, [input4u]
			cmp ebx, [input5u]		; if A[4] > A[5]
			jbe after_if4u
			mov ebx, [input4u]			; swap ( A[4], A[5] )
			mov ecx, [input5u]
			mov [input4u], ecx
			mov [input5u], ebx
			mov edx, 1  
		
		after_if4u:			; Output results
			
			mov eax, [input1u]
			call print_int
			mov eax, outmsg1
			call print_string
			
			mov eax, [input2u]
			call print_int
			mov eax, outmsg1
			call print_string
			
			mov eax, [input3u]
			call print_int
			mov eax, outmsg1
			call print_string
			
			mov eax, [input4u]
			call print_int
			mov eax, outmsg1
			call print_string
			
			mov eax, [input5u]
			call print_int
			mov eax, outmsg1
			call print_string

			call print_nl
			
			cmp edx, 1		; Repeat if anything was previously swapped
			je while_loopu
	

	popa
	mov eax, 0		; return back to c
	leave
	ret